package com.choosefine.base.shixiaobao.basedata.controller;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.choosefine.base.shixiaobao.base.BaseController;
import com.choosefine.base.shixiaobao.basedata.model.AttendanceCompany;
import com.choosefine.base.shixiaobao.basedata.service.AttendanceCompanyService;
import com.choosefine.base.shixiaobao.common.def.CommonConstant;
import com.choosefine.base.shixiaobao.common.def.ResultCode;
import com.choosefine.base.shixiaobao.common.exception.BusinessException;
import com.choosefine.base.shixiaobao.common.utils.DateUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * 考勤-公司详情相关控制层
 * 
 * @author guwenwei 2016/11/16
 */
@RestController
@RequestMapping("/basedata/attendancecompany")
@Api(value = "考勤-公司详情相关", description = "熊朝")
public class AttendanceCompanyController extends BaseController {
	Logger logger = LoggerFactory.getLogger(AttendanceCompanyController.class);
	@Autowired
	AttendanceCompanyService attendanceCompanyService;
	/**
	 * 考勤-添加公司详情
	 * 
	 * @param companyinfo
	 * @param
	 * @return
	 * @throws BusinessException 
	 * @throws Exception
	 */
	@ApiOperation(value = "存储考勤-公司详情", notes = "传入考勤-公司对象，存储考勤-公司详情数据。")
	@ResponseBody
	@ApiResponses({
			@ApiResponse(code = ResultCode.SUCCESS_CREATED, message = "添加成功返回结果"),
			@ApiResponse(code = ResultCode.FAILED, message = "请求参数错误") })
	@RequestMapping(method = RequestMethod.POST)
	public String save(@Valid @RequestBody AttendanceCompany attendanceCompany) throws BusinessException {
		String result = null;
		if (attendanceCompany != null) {
			attendanceCompany.setCtime(DateUtils.getNowTime());
			attendanceCompany.setIsValid(CommonConstant.VALID);
			int num = attendanceCompanyService.insert(attendanceCompany);
			logger.info("savecompanyinfo================="+ attendanceCompany.toString());
			if (num > 0) {
				result = responseSuccess(attendanceCompany);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(messageSourceUtils.getMessage("attendance.exception.msg_params_null"));
		}
		return result;
	}
	
	@ApiOperation(value = "修改公司详情", notes = "传入公司对象，更改公司详情数据。")
	@ResponseBody
	@RequestMapping(method = RequestMethod.PUT)
	public String update(@Valid @RequestBody AttendanceCompany attendanceCompany) {
		String result = null;
		if (attendanceCompany != null) {
			attendanceCompany.setUtime(DateUtils.getNowTime());
			int num = attendanceCompanyService.update(attendanceCompany);
			if (num > 0) {
				result = responseSuccess(num);
			} else {
				result = responseFail(ResultCode.MSG_ERROR);
			}
		} else {
			result = responseFail(messageSourceUtils.getMessage("attendance.exception.msg_params_null"));
		}
		return result;
	}
}
